/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.web.shell.config;

import cn.hutool.core.collection.CollectionUtil;
import cn.star.framework.core.IEntity;
import cn.star.framework.core.IRepository;
import cn.star.framework.rbac.repository.MenuRepository;
import cn.star.framework.rbac.repository.RoleRepository;
import cn.star.framework.rbac.repository.RoleResourceRepository;
import cn.star.framework.rbac.repository.UserRepository;
import cn.star.framework.rbac.repository.UserRoleRepository;
import java.io.Serializable;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;
import org.springframework.transaction.annotation.Transactional;

/**
 * 初始化<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Slf4j
@ShellComponent
public class Initialization {
  @Autowired private Config config;

  @Autowired private UserRepository userRepository;
  @Autowired private RoleRepository roleRepository;
  @Autowired private UserRoleRepository userRoleRepository;
  @Autowired private MenuRepository menuRepository;
  @Autowired private RoleResourceRepository roleresourceRepository;

  private static final String SYS_TYPE = "sys";
  private static final String XXL_TYPE = "xxl";
  private static final String ALL_TYPE = "all";

  @ShellMethod(
      key = "init",
      value =
          "initialization operation.\n"
              + "      This command used for initializing master data and scheduling platform basic data.\n"
              + "      Usage: init [OPTION]")
  @Transactional(rollbackFor = Throwable.class)
  public void init(
      @ShellOption(
              help =
                  "Enter the type value to initialize the corresponding data, otherwise initialize all.",
              value = {SYS_TYPE, XXL_TYPE, ALL_TYPE})
          String type) {
    boolean enabled = config.isEnabled();
    if (!enabled) {
      log.warn("init config not enabled, please set [ rbac.init.enabled: true ]");
      return;
    }

    if (type == null) {
      throw new RuntimeException("Type is empty");
    } else if (SYS_TYPE.equals(type)) {
      this.rbac();
    } else if (XXL_TYPE.equals(type)) {
      this.xxl();
    } else if (ALL_TYPE.equals(type)) {
      this.rbac();
      this.xxl();
    } else {
      throw new RuntimeException("Not support type: " + type);
    }
  }

  private void rbac() {
    this.saveOrUpdate(userRepository, config.getUsers());
    this.saveOrUpdate(roleRepository, config.getRoles());
    this.saveOrUpdate(userRoleRepository, config.getUserRoles());
    this.saveOrUpdate(menuRepository, config.getMenus());
    this.saveOrUpdate(roleresourceRepository, config.getRoleResources());
  }

  private void xxl() {
    _default.xxl();
  }

  private <Id extends Serializable, T extends IEntity> void saveOrUpdate(
      IRepository<T, Id> repository, List<T> data) {
    if (CollectionUtil.isNotEmpty(data)) {
      repository.saveAll(data);
    }
  }

  @ShellMethod(key = "hello-world")
  @Transactional(rollbackFor = Throwable.class)
  public String helloWorld(@ShellOption String str) {
    return "Hello World " + str;
  }
}
